package com.everyline.baselibrary.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.customview.DialogCustomViewExtKt;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.everyline.baselibrary.R;
import com.everyline.baselibrary.http.ApiException;
import com.everyline.baselibrary.interfaceListener.DialogButtonClickListener;
import com.everyline.baselibrary.utils.FragmentUserVisibleController;

/**
 * crated by Administrator on 2021/6/7
 * 746608431@qq.com
 * 描述：TODO
 */
abstract public class BaseDBFragment<V extends ViewDataBinding> extends Fragment {
    protected V mViewDataBinding;
    protected Context mContext;
    protected Activity mActivity;
    private MaterialDialog loadingDialog;
    private MaterialDialog dialogTwoButton;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = context;
        mActivity = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mViewDataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        mViewDataBinding.setLifecycleOwner(this);
        return mViewDataBinding.getRoot();
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(savedInstanceState);
        initPresenter();
        initData();
        initLocation();
        initListener();
    }



    protected abstract int getLayoutId();


    protected abstract void initView(Bundle savedInstanceState);

    protected abstract void initPresenter();

    protected abstract void initData();

    protected void initLocation() {
    }


    protected abstract void initListener();


    private void showLoadingExt() {

        if (mActivity != null) {
            if (!mActivity.isFinishing()) {
                if (loadingDialog == null) {
                    loadingDialog = new MaterialDialog(mActivity, MaterialDialog.getDEFAULT_BEHAVIOR());
                    loadingDialog.cancelable(false);
                    loadingDialog.cancelOnTouchOutside(false);
                    loadingDialog.cornerRadius(12f, null);
                    DialogCustomViewExtKt.customView(loadingDialog, R.layout.layout_custom_progress_dialog_view, null, true, true, true, true);
                }
                loadingDialog.show();
            }

        }

    }

    private void dismissLoadingExt() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }

    protected void showDialogOneORTwoButton(String title, String message, String[] buttonNames, DialogButtonClickListener dialogButtonClickListener) {
        dialogTwoButton = new MaterialDialog(mContext, MaterialDialog.getDEFAULT_BEHAVIOR());
        dialogTwoButton.cancelable(false);
        dialogTwoButton.cancelOnTouchOutside(false);
        dialogTwoButton.cornerRadius(12f, null);
        DialogCustomViewExtKt.customView(dialogTwoButton, R.layout.dialog_two_button_layout, null, true, true, false, true);

        dialogTwoButton.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                TextView tv_dialog_title = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.tv_dialog_title);
                TextView tv_dialog_message = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.tv_dialog_message);
                TextView tv_dialog_cancle = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.tv_dialog_cancle);
                TextView tv_dialog_ok = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.tv_dialog_ok);
                View line_mid = DialogCustomViewExtKt.getCustomView(dialogTwoButton).findViewById(R.id.line_mid);
                tv_dialog_title.setText(title);
                tv_dialog_message.setText(message);
                if (buttonNames != null && buttonNames.length > 0) {
                    switch (buttonNames.length) {
                        case 2:
                            tv_dialog_cancle.setText(buttonNames[0]);

                            tv_dialog_cancle.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (dialogTwoButton != null) {
                                        dialogTwoButton.dismiss();
                                        dialogButtonClickListener.negativeButtonClick();
                                    }
                                }
                            });
                            tv_dialog_ok.setText(buttonNames[1]);
                            tv_dialog_ok.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (dialogTwoButton != null) {
                                        dialogTwoButton.dismiss();
                                        dialogButtonClickListener.positiveButtonClick();
                                    }
                                }
                            });
                            break;
                        case 1:

                            tv_dialog_cancle.setVisibility(View.GONE);
                            line_mid.setVisibility(View.GONE);
                            tv_dialog_ok.setText(buttonNames[0]);
                            tv_dialog_ok.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (dialogTwoButton != null) {
                                        dialogTwoButton.dismiss();
                                        dialogButtonClickListener.positiveButtonClick();
                                    }
                                }
                            });
                            break;
                    }
                }

            }
        });
        dialogTwoButton.show();
    }

    protected void handleThrowable(Throwable throwable) {
        throwable.printStackTrace();

        //todo  比如token 失效 或者封停用户 直接跳到 登录页   其他情况  可以在这里做统一处理

        if (throwable instanceof ApiException) {
            ApiException exception = (ApiException) throwable;
            String errorMsg = exception.getMsg();
            int err_code = exception.getCode();
            LogUtils.d("err_code===", err_code);
            if (err_code == 1001) {

            } else {
                if (!TextUtils.isEmpty(errorMsg)) {
                    ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).showShort(errorMsg);
                }
            }
        }
    }

    protected void showToast(String msg) {
        if (!StringUtils.isEmpty(msg)) {
            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0).showShort(msg);
        }

    }
}
